﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using UniCT.LinkShareWpf.Interface;
using System.IO;
using System.Collections.ObjectModel;

namespace LinkShareWpf
{
    /// <summary>
    /// Interaction logic for InsertResourceForm.xaml
    /// </summary>
    public partial class InsertResourceForm : Window
    {
            Resource resource;
            TechnicalInfo tinfo;
            ContentInfo cinfo ;
            ContentInfoTVS cinfoTVS;

        public InsertResourceForm()
        {
            InitializeComponent();

            
            // Il context per gli attributi della Risorsa
            resource = new Resource();
            resourceGBox.DataContext = resource;

            // Il context per gli attributi delle informazioni Tecniche
            tinfo = new TechnicalInfo();
            tinfoGBox.DataContext = tinfo;

            // Il context per gli attributi delle informazioni di Contenuto
            // Di default si pensa che la risorsa non sia una serie Tv
            cinfo = new ContentInfo();
            cinfoGBox.DataContext = cinfo;


            genreBox.ItemsSource = Enum.GetValues(typeof(EnumGenre));
            languageBox.ItemsSource = Enum.GetValues(typeof(EnumLanguage));
            subTitleBox.ItemsSource = Enum.GetValues(typeof(EnumLanguage));
            videoTypeBox.ItemsSource = Enum.GetValues(typeof(EnumVideoType));
            videoSourceBox.ItemsSource = Enum.GetValues(typeof(EnumVideoSource));
            audioSourceBox.ItemsSource = Enum.GetValues(typeof(EnumAudioSource));

            ratingFav.ValueChanged += new
            RoutedPropertyChangedEventHandler<double?>(ratingFav_ValueChanged);
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {

            resource.TInfo = tinfo;
            if (tvseriesCBox.IsChecked == true)
                     resource.CInfo = cinfoTVS;
             else
                    resource.CInfo=cinfo;

            

            //richTextBox1.Document.Blocks.Clear();
            //richTextBox1.AppendText(resource.Url);
            //richTextBox1.AppendText("," + resource.Rating);
            //richTextBox1.AppendText("," + resource.CInfo.Title);
            //richTextBox1.AppendText("," + resource.CInfo.Director);
            //richTextBox1.AppendText("," + resource.CInfo.Storyline);
            //richTextBox1.AppendText("," + resource.CInfo.Genre);
            //richTextBox1.AppendText("," + resource.CInfo.Language);
            //if (subTitleCBox.IsChecked == true)
            //    richTextBox1.AppendText("," + resource.CInfo.SubTitleLanguage);
            //else
            //    resource.CInfo.SubTitleLanguage = null;
            //richTextBox1.AppendText("," + resource.CInfo.Cast.Count);
            //if (tvseriesCBox.IsChecked == true)
            //{
            //    richTextBox1.Document.Blocks.Clear();
            //    richTextBox1.AppendText("," + cinfoTVS.EpisodNumber);
            //    richTextBox1.AppendText("," + cinfoTVS.SeasonNumber);
            //    richTextBox1.AppendText("," + cinfoTVS.EpisodTitle);
         
                

            //}


        }

        void ratingFav_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double?> e)
        {

              
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                dlg.ShowDialog();
                BitmapImage bitmap = new BitmapImage(new Uri(dlg.FileName));
                image1.Height = bitmap.Height;
                image1.Width = bitmap.Width;
                image1.Source = bitmap;
                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                String photolocation = "Cover/" + System.IO.Path.GetFileName(dlg.FileName);
                encoder.Frames.Add(BitmapFrame.Create((BitmapImage)bitmap));
                using (var filestream = new FileStream(photolocation, FileMode.Create))
                    encoder.Save(filestream);

                if (tvseriesCBox.IsChecked == true)
                {
                    cinfoTVS.CoverName = System.IO.Path.GetFileName(dlg.FileName); ;
                }
                else
                {

                    cinfo.CoverName = System.IO.Path.GetFileName(dlg.FileName); ;
                }
               

            }
            catch (Exception ee)
            {
              MessageBox.Show("Immagine non selezionata");
            }

        }

 
        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            String tmp = castTxt.Text;
            ContentInfo cinfo = (ContentInfo)cinfoGBox.DataContext;
            if (!tmp.Equals(String.Empty))
                cinfo.Cast.Add(tmp);
            castTxt.Text = String.Empty;


        }

        private void castTxt_TextChanged(object sender, TextChangedEventArgs e)
        {
            button1.IsEnabled = true;
            string tempText = ((TextBox)sender).Text;
            ContentInfo cinfo = (ContentInfo)cinfoGBox.DataContext;
            foreach (string actor in cinfo.Cast)
                if (actor.Equals(tempText))
                {
                    button1.IsEnabled = false;
                    break;
                }
        }
        public Resource getResource()
        {


            return (Resource)resourceGBox.DataContext;
        }

        private void tvseriesCBox_Unchecked(object sender, RoutedEventArgs e)
        {

        }

        private void tvseriesCBox_Checked(object sender, RoutedEventArgs e)
        {
            // Il context per gli attributi delle informazioni di Contenuto 
            // In questo caso la risorsa è una Serie Tv
            cinfoTVS = new ContentInfoTVS(cinfo);

            resource.CInfo = cinfoTVS;








            groupBtvserieGBox.DataContext = cinfoTVS;
            
        }

	}    
     
    
}
